<?php

namespace app\admin\controller\reply;

use app\admin\controller\Base;
use app\admin\model\ReplyMessage;
use think\cache\driver\Redis;
use think\Db;
use think\Request;

/**
 * @author zero
 * @Time 2018/12/13
 *
 * Class Index
 * @package app\admin\controller\reply
 *
 * 回复消息首页相关接口
 */
class Index extends Base
{

    // 回复消息库页面
    public function index()
    {
        return $this->fetch('replymessage/index');
    }

    // 获取回复消息列表
    public function get_msg_list()
    {
        $keyword = $this->request->post('keyword');
        $msg_list = ReplyMessage::whereLike('name', '%' . $keyword . '%')->select();
        foreach ($msg_list as $msg) {
            if ($msg->type == 'text') {
                $msg->type = '文本';
            } elseif ($msg->type == 'news') {
                $msg->type = '图文';
            }
        }
        return $msg_list;
    }

    // 添加客服消息界面
    public function handle_msg_view()
    {
        $msgid = $this->request->param('msgid');
        $this->assign('msgid', $msgid);
        return $this->fetch('replymessage/handle_msg');
    }

    // 获取当前回复消息
    public function get_msg()
    {
        $msgid = $this->request->param('msgid');
        $msg = ReplyMessage::get($msgid);
        if ($msg) {
            $type = $msg->type;

            // 判断redis里有没有信息回复内容
            $content = $this->get_content($msg->id);
            if ($content) {
                $msg->content = $content;
            }

            if ($type == 'news') {
                $msg->content = json_decode($msg->content);
            }
            return $msg;
        }
        return false;
    }

    // 处理回复消息(新增或修改)
    public function handle_msg()
    {
        // 获取POST数据
        $msgid = $this->request->param('msgid');
        $name = $this->request->post('name');
        $content = $this->request->post('content');
        $type = $this->request->post('type');
        $is_subscribe = $this->request->post('is_subscribe');
        $is_normal = $this->request->post('is_normal');
        $is_keyword = $this->request->post('is_keyword');
        // 拿到msg对象
        $msg = ReplyMessage::get($msgid);
        if (!$msg) {
            $msg = new ReplyMessage();
        }

        // 当为图文消息时,对内容格式处理
        if ($type == 'news') {
            $content = json_encode($content, JSON_UNESCAPED_UNICODE);
        }

        // 更新数据并返回保存结果
        $msg->name = $name;
        $msg->content = $content;
        $msg->type = $type;
        $msg->is_normal = $is_normal == 'true' ? 1 : 0;
        $msg->is_keyword = $is_keyword == 'true' ? 1 : 0;
        $msg->is_subscribe = $is_subscribe == 'true' ? 1 : 0;
        $msg->createtime = date('Y-m-d H:i:s', time());
        if ($msg->save()) {
            return $this->set_content($msg->id, $content);
        } else {
            return false;
        }
    }

    public function set_content($msgid, $content)
    {
        $redis = new Redis();
        return $redis->set($msgid . '_reply_msg', $content);
    }

    public function get_content($msgid)
    {
        $redis = new Redis();
        return $redis->get($msgid . '_reply_msg');
    }

    // 删除回复消息
    public function del_msg()
    {
        $msgid = $this->request->param('msgid');
        $msg = ReplyMessage::get($msgid);
        return $msg->delete();
    }
}
